package com.jojo.bubble.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;

/**
 * @author: jojo
 * @date: 2022/11/15 15:49
 * @version: 1.0
 */
@Slf4j
public class ImageTrimUtil {

    /**
     * @desc: 测试图片截边
     * @author: jojo
     * @date: 2022/11/15 15:51
     */
    public static void main(String[] args) throws IOException {
        // 选择图片
        File file1 = new File("E:/test.png");
        BufferedImage image1 = ImageIO.read(file1);
        // 输出图片(相同即覆盖)
        File outFile = new File("E:/test1.png");

        // 顶部空白高度
        int trimTop = trimColor(image1, "top", "white");
        // 底部空白高度
        int trimBottom = trimColor(image1, "bottom", "white");
        int trimLeft = trimColor(image1, "left", "white");
        int trimRight = trimColor(image1, "right", "white");
        log.info("top:" + trimTop + "bottom:" + trimBottom + "left:" + trimLeft + "right:" + trimRight);
        //上下
//        image1 = image1.getSubimage(0, trimTop, image1.getWidth(), image1.getHeight() - trimTop - trimBottom);
        // 上下左右
        image1 = image1.getSubimage(trimLeft, trimTop, image1.getWidth() - trimLeft - trimRight, image1.getHeight() - trimTop - trimBottom);
        ImageIO.write(image1, "png", outFile);
//        htmlToPDFToImg("");
    }


    /**
     * @param colorName 为空时默认white
     * @return color
     * 根据名称获取颜色
     */
    public static Color getColorByName(String colorName) {
        Color color;
        try {
            if (StringUtils.isEmpty(colorName)) {
                colorName = "white";
            }
            Field field = Color.class.getField(colorName);
            color = (Color) field.get(null);
        } catch (Exception e) {
            color = null; // Not defined
        }
        return color;
    }

    private static final String top = "top";
    private static final String bottom = "bottom";
    private static final String left = "left";
    private static final String right = "right";

    /**
     * @param image     需要处理的图片
     * @param position  "top/bottom/left/right"
     * @param colorName 需要截取的颜色 red/green/blue...
     * @return trimSize 需要截取的尺寸
     * 如果该X或Y轴上所有点的颜色都是colorName，就判定这一行或列需要截取，最后返回需要截取尺寸
     */
    public static int trimColor(BufferedImage image, String position, String colorName) {
        int trimSize = 0;
        Color trimColor = getColorByName(colorName);
        if (trimColor == null) {
            System.out.println("无法识别的颜色:" + colorName);
            return trimSize;
        }
        switch (position) {
            case top:
                for (int y = 0; y < image.getHeight(); y++) {
                    boolean flag = true;
                    for (int x = 0; x < image.getWidth(); x++) {
                        if (image.getRGB(x, y) != trimColor.getRGB()) {
                            flag = false;
                            break;
                        }
                    }
                    if (flag) {
                        trimSize++;
                    } else {
                        break;
                    }
                }
                break;
            case bottom:
                for (int y = image.getHeight() - 1; y >= 0; y--) {
                    boolean flag = true;
                    for (int x = 0; x < image.getWidth(); x++) {
                        if (image.getRGB(x, y) != trimColor.getRGB()) {
                            flag = false;
                            break;
                        }
                    }
                    if (flag) {
                        trimSize++;
                    } else {
                        break;
                    }
                }
                break;
            case left:
                for (int x = 0; x < image.getWidth(); x++) {
                    boolean flag = true;
                    for (int y = 0; y < image.getHeight(); y++) {
                        if (image.getRGB(x, y) != trimColor.getRGB()) {
                            flag = false;
                            break;
                        }
                    }
                    if (flag) {
                        trimSize++;
                    } else {
                        break;
                    }
                }
                break;
            case right:
                for (int x = image.getWidth() - 1; x >= 0; x--) {
                    boolean flag = true;
                    for (int y = 0; y < image.getHeight(); y++) {
                        if (image.getRGB(x, y) != trimColor.getRGB()) {
                            flag = false;
                            break;
                        }
                    }
                    if (flag) {
                        trimSize++;
                    } else {
                        break;
                    }
                }
                break;
            default:
                System.out.println("无法识别的位置：" + position);

        }
        return trimSize;
    }

    /**
     * @return int
     * @desc trimImage by position, colorName
     * @Date 2023/7/27 10:16
     * @Param [image, position, colorName]
     */
    public static int trimImage(BufferedImage image, String position, String colorName) {
        int trimSize = 0;
        Color trimColor = getColorByName(colorName);
        if (trimColor == null) {
            System.out.println("error color:" + colorName);
            return trimSize;
        }


        return trimSize;
    }

}
